package com.hqyj.controller;

import com.hqyj.dao.UserInfoDao;
import com.hqyj.pojo.ManagerInfo;
import com.hqyj.pojo.UserInfo;
import com.hqyj.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;

@Controller
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    //创建一个userinfoservice的实现类对象
    @Autowired
    UserInfoService userInfoService;
    //接收用户发送的用户登录信息，用户名和密码
    //model用来把服务器的值传到前端
    @Autowired
    UserInfoDao userInfoDao;


    //获取图片上传的配置路劲
    @Value("${file.address}")
    String filrAdress;
    //用户访问的图片路径
    @Value("${file.staticPath}")
    String upload;


    @RequestMapping("/loginform")
    public String loginForm(UserInfo user, ManagerInfo man, Model map, HttpServletRequest request){
        String info = userInfoService.login(user,man,request);

        map.addAttribute("info",info);
        System.out.print(info);
  //返回登录页面
        return "login";
}
    //定义一个
    @RequestMapping("/loginpage")
    public String loginPage(){


        return "login";
    }
    //ajax登录
    @RequestMapping("/loginAjax")
    @ResponseBody
    public HashMap<String,Object> loginajax(UserInfo user,ManagerInfo man, HttpServletRequest request){
        HashMap<String,Object> map = new HashMap<>();
        String info = userInfoService.login(user,man, request);
        map.put("info",info);
        return map;

    }

    //访问注册页面
    @RequestMapping("/zhucepage")
    public String zhucepage(){return "zhuce";}
    @RequestMapping("/zhuce")
    @ResponseBody
    public HashMap<String,Object> zhuce(UserInfo user){
        HashMap<String,Object> map = new HashMap<>();
        System.out.print(user.getUserPhoto()+"********************************");
        //访问注册方法
        String info = userInfoService.zhuce(user);
        map.put("info",info);
        return map;
    }

    //上传图片
    @RequestMapping("/upload")
    @ResponseBody
    public HashMap<String,Object> upload(MultipartFile file){
        HashMap<String,Object> map = new HashMap<>();

        //上传文件
        try {
            //定义一个文件上传的目录
            String timeDir = "";
            //获取事件
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            timeDir = sdf.format(new Date());


            //定义上传文件的前缀
            String pre ="";
            //是为了保证文件上传后，存到服务器的文件名是唯一的
            pre = UUID.randomUUID()+"";
            //获取文件的后缀
            //获取上传图片的后缀
            String hou="";
            if(file!=null) {
                //。jpg
                String originalName = file.getOriginalFilename();
                hou = originalName.substring(originalName.lastIndexOf(".") + 1);
            }
            //定义文件上传的全路劲



            String filePath=filrAdress+"\\"+timeDir+"\\"+pre+"."+hou;


            //创建file对象
            File f= new  File(filePath);

            //判断目录是否存在，不存在创建目录
            if(!f.isDirectory()){
                //创建目录
                f.mkdirs();

            }
            file.transferTo(f);
            //设置上传提示信息
            map.put("code",0);
            //返回给前端 用户访问这个图片的路
            String path = upload+"\\"+timeDir+"\\"+pre+"."+hou;
            //
            map.put("src",path);
            return map;
        } catch (IOException e) {
            e.printStackTrace();
        }
        map.put("code",1);
        return map;

    }





    //处理邮件发送的请求

    @RequestMapping("/sendEmail")
    @ResponseBody
    public HashMap<String,Object> sendEmail(String email, HttpServletRequest request){
        UserInfo user=new UserInfo();
        user.setUserEmail(email);
        //查询当前用户
        UserInfo userInfo=userInfoDao.selecte(user);
        //创建session对象
        HttpSession sessions = request.getSession();
        //存入用户对象
        sessions.setAttribute("user", userInfo);
        return userInfoService.sendCode(email,request);
    }
    //处理邮件登录的请求
    @RequestMapping("/emaillogin")
    @ResponseBody
    public HashMap<String,Object> emaillogin(String code,HttpServletRequest request){
        HashMap<String,Object> map = new HashMap<>();
        //查询当前用户

        //判断用户输入的验证码和邮箱接收的验证码是否一致
        //获取session对象
        HttpSession session = request.getSession();
        //取出存到session中的验证码的值
        String valCode = session.getAttribute("valCode")+"";

        System.out.print(code+"+123344444444444444444444444444444");
        if(code.equals(valCode)){
//获取session
            UserInfo u = (UserInfo) session.getAttribute("user");
            System.out.print(u.getUserName()+"+123344444444444444444444444444444");
            map.put("info","邮箱登录成功");

        }else {

            map.put("info","验证码输入错误");
        }
        return map;

    }
}
